﻿// <copyright file="Categories.cs" company="Auros Ltd.">
//     Copyright (c) Auros Ltd. All rights reserved.
// </copyright>

namespace Auros.Categorisation
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Auros.Categorisation.Data;

    /// <summary>
    /// Class containing methods which give access to the categories stored within the Umbraco database.
    /// </summary>
    public class Categories
    {
        /// <summary>
        /// Gets the categories within the system. If a parent ID is specified, only descendants of that category are returned.
        /// </summary>
        /// <param name="parentId">The root parent ID (optional).</param>
        /// <returns>Collection of category objects.</returns>
        public static Category[] GetCategories(int parentId = -1)
        {
            return new CategoryRepository().GetCategories(parentId)
                .Where(c => c.Enabled)
                .OrderBy(c => c.SortOrder)
                .ToArray();
        }

        /// <summary>
        /// Gets all categories (including disabled) within the system.
        /// </summary>
        /// <param name="parentId">The root parent ID (optional).</param>
        /// <returns>Collection of category objects.</returns>
        public static Category[] GetAllCategories(int parentId = -1)
        {
            return new CategoryRepository().GetCategories(parentId)
                .OrderBy(c => c.SortOrder)
                .ToArray();
        }

        /// <summary>
        /// Gets the category with the specified ID.
        /// </summary>
        /// <param name="categoryId">The category ID.</param>
        /// <returns>The category object with the specified ID. Null if no category is found.</returns>
        public static Category GetCategory(int categoryId)
        {
            return new CategoryRepository().GetCategory(categoryId);
        }

        /// <summary>
        /// Gets the categories with the specified IDs.
        /// </summary>
        /// <param name="categoryIds">The IDs of the categories.</param>
        /// <returns>Collection of category objects.</returns>
        public static IEnumerable<Category> GetCategories(int[] categoryIds)
        {
            CategoryRepository categoryRepo = new CategoryRepository();

            foreach (var id in categoryIds)
            {
                var category = categoryRepo.GetCategory(id);

                if (category != null)
                    yield return category;
            }
        }

        /// <summary>
        /// Gets all categories with specified name.
        /// </summary>
        /// <param name="categoryName">Name of category to find.</param>
        /// <param name="rootId">Root category to search under.</param>
        /// <param name="comparisonType">String comparison type.</param>
        /// <returns>Collection of categories found with the specified name.</returns>
        public static IEnumerable<Category> FindCategoryByName(string categoryName, int rootId = -1, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
        {
            return Categories.GetAllCategories(rootId)
                .Where(c => c.Name.Equals(categoryName, comparisonType));
        }
    }
}